/**
 * Created by yaohuan on 2018/4/17 0017.
 */
define(function () {
    // 全局遮罩
    var mask;
    var self, pjjsData;
    var list = [];


    function pjjs() {
        self = this;
        //self.productData = [];
        self.mData = {
            // option:[],//选择客户和维修单号
            // imgs: [],
            tabType: ['1'],
            orderSet: {},
        }
        self.init();
    }

    //数据获取
    pjjs.prototype.getData = function (goods) {

        app.ajax({
            xurl: 'api/wx/pjjs/getPartListData',
            xdata: {
                page:goods.page+1,
                limit: goods.limit
            },
            cb: function (result) {
                if (result.ok) {
                    if (goods.page == 0) {
                        goods.data.removeAll()
                    }
                    var data_ = result.data.data;
                    goods.data.pushArray(data_.addMap({checked: false, MMRU005: 1}));

                    var refresh = goods.refresh;
                    if (goods.page == 0) {
                        refresh.endPulldownToRefresh();
                        if (result.data.data.length == 0) {
                            app.showNoData(goods._tip, '您还没有订单...');
                            refresh.disablePullupToRefresh();
                        } else {
                            app.hideNoData(goods._tip);
                            refresh.refresh(true);
                        }
                    }
                    goods.page += 1;
                    refresh.endPullupToRefresh(result.data.data < goods.limit);
                }
            }
        })
    };

    //获取配件列表
    pjjs.prototype.init = function () {

        mask = mui.createMask();



        mui.ready(function () {
            // list列表
            Vue.component('pjjs-listitem', {
                template: app.getByid(doc, 'item-template'),
                props: {
                    model: Array,

                },
                methods: {
                    minus: function (obj) {
                        if (obj.MMRU005 > 1) {
                            obj.MMRU005--;
                        }
                    },
                    plus:function (obj) {
                        if(obj.MMRU005<obj.MMRT006){
                            obj.MMRU005++;
                        }
                    }
                },
                created: function () {
                }
            });

            //底部按钮
            new Vue({
                el: document.getElementById("footdiv"),
                data: function () {

                },
                methods: {
                    edit: function () { //编辑
                        var check = 0
                        for (var i = 0; i < pjjsData.items.length; i++) {
                            if (pjjsData.items[i].checked == true) {
                                check++;
                            }
                        }
                        if (check == 0) {
                            app.toast('请选择需要寄送的物料');
                            return;
                        }
                        app.openPage({
                            src: "pjjs/pjjs-info.html",
                            argsKey: 'pjjsinfo_page',
                            args: pjjsData
                        });
                    }
                }
            })

            // 下拉组件
            var refreshData = function (i) {
                var _type = self.mData.tabType[i];
                var goods = self.mData.orderSet[_type];
                var option = self.mData.option;
                if (!goods) {
                    goods = self.mData.orderSet[_type] = {
                        page: 1,
                        limit: 10,
                        data: [],
                        start: function () {
                            return this.page * this.limit;
                        },
                        type: _type
                    }
                    // 绑定列表容器
                    pjjsData = new Vue({
                        el: doc.getElementById('pjjs'),
                        data: {
                            items: goods.data
                        }
                    });

                    goods._tip = $('#pullRefresh');
                    goods.refresh = mui('#pullRefresh').pullRefresh({
                        down: {
                            callback: function () {
                                goods.page = 0;
                                self.getData(goods);
                            },
                            auto: true
                        }, up: {
                            callback: function () {
                                self.getData(goods)
                            }
                        }
                    })
                }

            }
            refreshData(0);

            setTimeout(function () {
                $('#allCheck').click(function () {
                    var goods = self.mData.orderSet[1];
                    if ($('#allCheck').is(':checked')) {
                        $('#pullRefresh input[type=checkbox]').prop('checked', true);
                        for (var i = 0; i < pjjsData.items.length; i++) {
                            pjjsData.items[i].checked = true;
                        }
                    } else {
                        $('#pullRefresh input[type=checkbox]').prop('checked', false);
                        for (var i = 0; i < pjjsData.items.length; i++) {
                            pjjsData.items[i].checked = false;
                        }
                    }
                });

            }, 200);
        })
    }

    return new pjjs();
});